/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import entity.Feedback;
import entity.UserInfo;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import model.FeedbackFacade;
import model.UserInfoFacade;

/**
 *
 * @author VietTien
 */
@ManagedBean
@SessionScoped
public class feedbackController {

    @EJB
    private UserInfoFacade userInfoFacade;
    @EJB
    private FeedbackFacade feedbackFacade;
    private DataModel item = null;
    private Feedback current = new Feedback();
    private UserInfo user;

    /** Creates a new instance of feedbackController */
    public feedbackController() {
    }

    public Feedback getCurrent() {
        return current;
    }

    public void setCurrent(Feedback current) {
        this.current = current;
    }

    public FeedbackFacade getFeedbackFacade() {
        return feedbackFacade;
    }

    public void setFeedbackFacade(FeedbackFacade feedbackFacade) {
        this.feedbackFacade = feedbackFacade;
    }

    public DataModel getItem() {
        if (item == null) {
            item = new ListDataModel(feedbackFacade.findAll());
        }
        return item;
    }

    public void setItem(DataModel item) {
        this.item = item;
    }

    public UserInfo getUser() {
        return user;
    }

    public void setUser(UserInfo user) {
        this.user = user;
    }

    public UserInfoFacade getUserInfoFacade() {
        return userInfoFacade;
    }

    public void setUserInfoFacade(UserInfoFacade userInfoFacade) {
        this.userInfoFacade = userInfoFacade;
    }

    public String delete() {
        current = (Feedback) getItem().getRowData();
        performDestroy();
        recreateModel();
        return "List";
    }

    private void performDestroy() {
        try {
            feedbackFacade.remove(current);
            JsfUtil.addSuccessMessage("Feedback successfull deleted");
        } catch (Exception e) {
            JsfUtil.addErrorMessage("Feedback delete fail !");

        }
    }

    private void recreateModel() {
        item = null;
    }

    public String prepareList() {
//        current = (Feedback) getItem().getRowData();
        return "List";
    }

    public String prepareView() {
        current = (Feedback) getItem().getRowData();
        return "View";
    }

    public String prepareSend() {

        current = (Feedback) getItem().getRowData();
        return "Answer";
    }

    public String update() {
        try {



            feedbackFacade.editFB(current);
            JsfUtil.addSuccessMessage("Update successfull !");
            recreateModel();
            return "List";

        } catch (Exception e) {
            e.printStackTrace();
            JsfUtil.addErrorMessage("Error in update !");
            return null;
        }
    }

    public List<Feedback> getListFB() {
        List<Feedback> lstFeedback = null;

        user = (UserInfo) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");

        if (user != null) {
            lstFeedback = userInfoFacade.findAllFeedBack(user);
        }
        return lstFeedback;
    }

    public String insertFB() {
        if (JsfUtil.isSpacesOnly(current.getTitle().trim())) {
                JsfUtil.addErrorMessage("Please input title field.");
                return null;
            }
        if (JsfUtil.isSpacesOnly(current.getContent().trim())) {
                JsfUtil.addErrorMessage("Please input content field.");
                return null;
            }
        user = (UserInfo) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
        current.setCommentDate(new java.util.Date());
        current.setUserInfo(user);
        feedbackFacade.create(current);
        JsfUtil.addSuccessMessage("Send FeedBack Successfully !");
        recreateModel();
        return "ListFB";
    }

    public String prepareSendFB() {
        current = new Feedback();
        return "Send";
    }

    public String prepareListFB() {
        recreateModel();
        return "ListFB";
    }

    public String prepareViewFB(Feedback fb) {
        current = fb;
        return "ViewFB";
    }

    public String navigate(String page) {
        return page;
    }
}
